/**
 * Created by Administrator on 2017/12/31.
 */
/**
 * 页面资源权限控制JS
 */
/**
 * Created by Administrator on 2017/12/27.
 * 菜单管理JS
 */
layui.config({
    base: '../build/js/'
}).use(['ztree', 'layer','table','form'], function() {

    var $ = layui.jquery,
        layer = layui.layer
        ,table = layui.table
        ,form = layui.form
        ,addResourceLayer=null
        ,selectNode=null;

    /**
     * 加载菜单树
     * @type {Array}
     */
    var zNodes = [];
    var queryParam={menuId:-1};
    var setting = {
        view: {
            selectedMulti: false
        },
        check: {
            enable: false
        },
        data: {
            simpleData: {
                enable: true
            }
        },
        edit: {
            enable: false
        },
        callback: {
            onClick: zTreeOnClick
        }
    };

    //初始化树
    function initZTree() {
        $.post("/sysMenu/menuTree", {systemId:"upms",type:0}, function(res) {
            if(res){
                zNodes=res;
                var zTreeObj =$.fn.zTree.init($("#ztree"), setting, zNodes);
                zTreeObj.expandAll(true);
            }
        }, 'json');
    }

    function zTreeOnClick(event, treeId, treeNode) {
        selectNode=treeNode;
        queryParam.menuId=treeNode.id;
        table.reload('menuResourcesTable', {
            where: queryParam
        });
    };

    $(document).ready(function() {
        initZTree();
    });

    //加载子级菜单列表
    table.render({
        elem: '#menuResources'
        ,url:'../sysResource/selectResources'
        ,where:queryParam
        ,method:'post'
        ,cellMinWidth: 80
        ,cols: [[
            {type: 'checkbox'}
            ,{field:'name', title:'名称',edit: 'text'}
            ,{field:'permission', title: '权限',minWidth:120,edit: 'text'}
            ,{field:'type', title: '类型',minWidth:120,templet: '#showTypeTpl'}
            ,{fixed: 'right', width:100, align:'center', toolbar: '#barMenuResources'}
        ]]
        ,id: 'menuResourcesTable'
    });

    var active = {
        addResource: function(){
            addResourceLayer=layer.open({
                type:1,
                area:'30%',
                offset:'20%',
                maxmin:true,
                skin:'layui-layer-molv',
                content:$("#addResourcesForm")
            });
        }
    };

    //添加
    form.on('submit(addResourceSubmit)', function(data) {
        var loadIndex = layer.load(2, {
            shade: [0.3, '#333']
        });
        if(queryParam.menuId==-1){
            layer.msg("请选择菜单", {
                icon: 2,
                time:3000
            });
            loadIndex && layer.close(loadIndex);
            return;
        }
        data.field.pId=queryParam.menuId;
        data.field.type=1;
        data.field.isLeaf=1;
        data.field.systemId=selectNode.systemId;
        $.post("../sysResource/addSysResource", data.field, function(res) {
            loadIndex && layer.close(loadIndex);
            if(res.code==0){
                layer.msg(res.msg, {
                    icon: 1,
                    time:3000
                });
                layer.close(addResourceLayer);
                table.reload('menuResourcesTable', {
                    where: queryParam
                });
            }else{
                layer.msg(res.msg, {
                    icon: 2,
                    time:3000
                });
            }
        }, 'json');
    });

    //监听单元格编辑
    table.on('edit(menuResources)', function(obj){
        var value = obj.value //得到修改后的值
            ,data = obj.data //得到所在行所有键值
            ,field = obj.field; //得到字段
        data[field]=value;
        $.post("/sysResource/updateSysResource",data, function(res) {
            if(res){
                if(res.code==0){
                    table.reload('menuResourcesTable', {
                        where: queryParam
                    });
                }else{
                    layer.msg(res.msg, {
                        icon: 2
                    });
                }
            }
        }, 'json');
    });

    //监听工具条
    table.on('tool(menuResources)', function(obj){
        var data = obj.data;
        if(obj.event === 'del'){
            layer.confirm('真的删除行么', function(index){
                $.post("/sysResource/deleteSysResource",{id:data.id}, function(res) {
                    if(res){
                        if(res.code==0){
                            layer.msg(res.msg, {
                                icon: 1,
                                time: 3000 //3s后自动关闭
                            });
                            table.reload('menuResourcesTable', {
                                where: queryParam
                            });
                        }else{
                            layer.msg(res.msg, {
                                icon: 2
                            });
                        }
                    }
                }, 'json');
            });
        }
    });

    $('.menuResourceToolBar .layui-btn').on('click', function(){
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });
});
